package other.process;

import org.geotools.data.DataStore;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.geometry.jts.Geometries;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiLineString;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.feature.type.GeometryType;
import utilityTools.ShpReaderUtil;

import java.io.File;

/**
 * @Author jlj
 * @Date 2022-06-07 14:30
 * @Description 计算路网数据的相关字段值
 * @Modified
 */
public class CalcRoadField {

    /**
     * 编辑一个shp文件
     * （必须用下面的方式：否则即使setAttributes了，内容也不会被保存）
     *
     * @throws Exception
     */
    public static void calcSpeedTime() throws Exception {
        String shpPath = "E:\\5.自研产品研发_2022\\路网分析服务\\Sample01\\RoadChangan\\road04.shp";
        String fieldSpeed = "maxspeed", fieldLevel = "code", fieldLevelDescription = "fName";
        SimpleFeatureSource sfsRoad = ShpReaderUtil.getShpfileSource(new File(shpPath), "GBK");
        FeatureWriter<SimpleFeatureType, SimpleFeature> edit = ((DataStore) sfsRoad.getDataStore()).getFeatureWriter(
                sfsRoad.getSchema().getTypeName(), Transaction.AUTO_COMMIT);
        int count = 0;
        while (edit.hasNext()) {
            SimpleFeature feature = edit.next();
            switch (feature.getAttribute(fieldLevel).toString()) {
                case "5111"://motorway
                    feature.setAttribute(fieldLevelDescription, "高速");
                    feature.setAttribute(fieldSpeed, 120);
                    break;
                case "5131"://motorway_link
                    feature.setAttribute(fieldLevelDescription, "高速引线");
                    feature.setAttribute(fieldSpeed, 40);
                    break;
                case "5132"://trunk_link
                case "5133"://primary_link
                case "5134"://secondary_link
                case "5135"://tertiary_link
                    feature.setAttribute(fieldLevelDescription, "等级道路引线");
                    feature.setAttribute(fieldSpeed, 40);
                    break;
                case "5112"://trunk
                    feature.setAttribute(fieldLevelDescription, "快速路");
                    feature.setAttribute(fieldSpeed, 80);//取速度区间的中间值
                    break;
                case "5113"://primary
                    feature.setAttribute(fieldLevelDescription, "一级");
                    feature.setAttribute(fieldSpeed, 50);
                    break;
                case "5114"://secondary
                    feature.setAttribute(fieldLevelDescription, "二级");
                    feature.setAttribute(fieldSpeed, 40);
                    break;
                case "5115"://tertiary
                    feature.setAttribute(fieldLevelDescription, "三级");
                    feature.setAttribute(fieldSpeed, 30);
                    break;
                case "5122"://residential
                    feature.setAttribute(fieldLevelDescription, "居民区内部道路");
                    feature.setAttribute(fieldSpeed, 20);
                    break;
                case "5123"://living_street
                    feature.setAttribute(fieldLevelDescription, "生活道路");
                    feature.setAttribute(fieldSpeed, 20);
                    break;
                case "5124"://pedestrian
                case "5153"://footway
                case "5154"://path
                    feature.setAttribute(fieldLevelDescription, "步行道");
                    feature.setAttribute(fieldSpeed, 5);
                    break;
                case "5141"://service
                    feature.setAttribute(fieldLevelDescription, "辅道");
                    feature.setAttribute(fieldSpeed, 40);
                    break;
                case "5142"://track
                    feature.setAttribute(fieldLevelDescription, "轨道交通");
                    feature.setAttribute(fieldSpeed, 60);
                    break;
                case "5155"://steps
                    feature.setAttribute(fieldLevelDescription, "台阶");
                    feature.setAttribute(fieldSpeed, 2);
                    break;
                case "5199"://unknown
                case "5121"://unclassified
                    feature.setAttribute(fieldLevelDescription, "未知等级道路");
                    feature.setAttribute(fieldSpeed, 2);
                    break;
            }
            edit.write();
            count++;
        }
        edit.close();
        System.out.println("赋值完毕，一共操作要素[" + count + "]个");
        sfsRoad.getDataStore().dispose();
    }

    public static void getShpClass() throws Exception {
        String shpDandong = "E:\\5.自研产品研发_2022\\路网分析服务\\Sample01\\RoadDandong\\road01.shp";
        String shpChangan = "E:\\5.自研产品研发_2022\\路网分析服务\\Sample01\\RoadChangan\\road02.shp";
        String shpChangan03 = "E:\\5.自研产品研发_2022\\路网分析服务\\Sample01\\RoadChangan\\road04.shp";
        SimpleFeatureSource sfsDandong = ShpReaderUtil.getShpfileSource(new File(shpChangan03), "GBK");
        SimpleFeatureIterator iterator_Dandong = sfsDandong.getFeatures().features();
        System.out.println("======");
        while (iterator_Dandong.hasNext()) {
            SimpleFeature feature = iterator_Dandong.next();
            //if (feature.getAttribute("gml_id").toString().equals("v2_layer_road_pg.875542")) {
            if (feature.getAttribute("ORIG_FID").toString().equals("34")) {
                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                System.out.println(Geometries.get(geometry));
                System.out.println(geometry.getGeometryType());
                System.out.println(geometry.getDimension());
                System.out.println(geometry.getNumGeometries());
                System.out.println(geometry.isSimple());
                break;
            }
        }
        GeometryDescriptor geoDescriptor_Dandong = sfsDandong.getSchema().getGeometryDescriptor();
        System.out.println("isAbstract:" + geoDescriptor_Dandong.getType().isAbstract());
        System.out.println(geoDescriptor_Dandong.getMaxOccurs());
        System.out.println(geoDescriptor_Dandong.getMinOccurs());
        System.out.println("丹东01：" + geoDescriptor_Dandong.getLocalName());
        System.out.println("丹东的几何对象类型：" + geoDescriptor_Dandong.getType().toString());
        System.out.println("=======");
        SimpleFeatureSource sfsChangan = ShpReaderUtil.getShpfileSource(new File(shpChangan), "GBK");
        GeometryDescriptor geoDescriptor_Changan = sfsChangan.getSchema().getGeometryDescriptor();
        SimpleFeatureIterator iterator_Changan = sfsChangan.getFeatures().features();
        while (iterator_Changan.hasNext()) {
            SimpleFeature feature = iterator_Changan.next();
            if (feature.getAttribute("ORIG_FID").toString().equals("34")) {
                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                System.out.println(geometry.getDimension());
                System.out.println(geometry.getNumGeometries());
                System.out.println(geometry.isSimple());
                break;
            }
        }
        System.out.println("长安01：" + geoDescriptor_Changan.getLocalName());
        System.out.println("isAbstract" + geoDescriptor_Changan.getType().isAbstract());
        System.out.println(geoDescriptor_Changan.getMaxOccurs());
        System.out.println(geoDescriptor_Changan.getMinOccurs());
        System.out.println("长安的几何对象类型：" + geoDescriptor_Changan.getType().toString());

    }

    /*
     * 验证结果：
     * 重要：shp类型的数据源是无法获取LineString类型的，只能看出来是Multi点线面的哪一种
     * （1）即使是自建的五角星路网，获取的几何类型也是MultiLineString类型
     * （2）丹东的路网数据几何类型仍然是MultiLineString，其中有9个多体对象，一个要素包含了多个几何体；
     * （3）长安路网数据处理并导出后仍然是MultiLineString类型，但是04的那个数据已经没有多体对象；
     *     现在将长安路网04作为可用数据
     * */
    public static void getGeometryType_Shp() throws Exception {
        //先验证一下获取的类型和几何的数量
        String shpPath = "E:\\5.自研产品研发_2022\\路网分析服务\\Sample01\\RoadChangan\\road04.shp";
        String shpName = shpPath.substring(shpPath.lastIndexOf('\\')+1);
        System.out.println("数据源："+shpName);
        SimpleFeatureSource sfsShp = ShpReaderUtil.getShpfileSource(new File(shpPath), "GBK");
        System.out.println("shp路网的原始数据量：" + sfsShp.getFeatures().size());
        SimpleFeatureType schema = sfsShp.getSchema();
        GeometryType geoType_FromSchema = schema.getGeometryDescriptor().getType();
        System.out.println("From Schema: " + geoType_FromSchema.getBinding());
        System.out.println("从schema获取的几何类型：" + geoType_FromSchema.getName().getLocalPart());
        SimpleFeatureIterator featureIterator = sfsShp.getFeatures().features();
        int count = 0,countMultiPart=0;
        while (featureIterator.hasNext()) {
            count++;
            SimpleFeature perFeature = featureIterator.next();
            Geometry perGeometry = (Geometry) perFeature.getDefaultGeometry();
            if (count < 3) {
                System.out.println("Geometries.get()： " + Geometries.get(perGeometry));
                System.out.println("当前要素包含的几何体数量： " + perGeometry.getNumGeometries());
            } else if (perGeometry.getNumGeometries() > 1) {
                countMultiPart++;
                System.out.println("Geometries.get()： " + Geometries.get(perGeometry));
                System.out.println("当前要素包含的几何体数量：" + perGeometry.getNumGeometries());
            }
        }
        System.out.println("Count of MultiPart: "+countMultiPart);
        System.out.println("===流程完毕===");
    }
}
